package app.caihan.scframe.ktx

import android.app.Activity
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment

/**
 *
 *
 * @author caihan
 * @date 2020/8/24
 * @e-mail 2730045482@qq.com
 * 维护者
 */

/**
 * 判断是否为空 并传入相关操作
 */
inline fun <reified T> T?.notNull(notNullAction: (T) -> Unit, nullAction: () -> Unit = {}) {
    if (this != null) {
        notNullAction.invoke(this)
    } else {
        nullAction.invoke()
    }
}

/**
 * Inflate a new view hierarchy from the specified xml node. Throws
 * {@link InflateException} if there is an error. *
 * <p>
 * <em><strong>Important</strong></em>&nbsp;&nbsp;&nbsp;For performance
 * reasons, view inflation relies heavily on pre-processing of XML files
 * that is done at build time. Therefore, it is not currently possible to
 * use LayoutInflater with an XmlPullParser over a plain XML file at runtime.
 *
 * @param parser XML dom node containing the description of the view
 *        hierarchy.
 * @param root Optional view to be the parent of the generated hierarchy.
 * @return The root View of the inflated hierarchy. If root was supplied,
 *         this is the root View; otherwise it is the root of the inflated
 *         XML file.
 */
fun Activity.inflateKtx(
    @LayoutRes resource: Int,
    root: ViewGroup? = null,
    attachToRoot: Boolean = false
): View {
    if (root == null) return layoutInflater.inflate(resource, null)
    return layoutInflater.inflate(resource, root, attachToRoot)
}

fun Fragment.inflateKtx(
    @LayoutRes resource: Int,
    root: ViewGroup? = null,
    attachToRoot: Boolean = false
): View {
    if (root == null) return layoutInflater.inflate(resource, null)
    return layoutInflater.inflate(resource, root, attachToRoot)
}